/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/11/24
 * Time: 05:50
 */
$(function(){
    var authorization = Cookies.get('_token');
    $.ajax({
        type: 'get',
        url: '/v1/teacher/get/file.json',
        headers: {
            Authorization: authorization,
            Accept: "application/json; charset=utf-8"
        },
        success: function(e){
            var data = JSON.parse(e);
            if (data.status == 200) {
                var json = data.body.data;
                if (json) {
                    $('.realname').val(json.realname);
                    if (json.gender == 1) {
                        $('input[name="gender"]:first').prop('checked', 'checked');
                    } else {
                        $('input[name="gender"]:last').prop('checked', 'checked');
                    }
                    $('.number').val(json.number);
                    $('.identity').val(json.identity);
                    $('.mobile').val(json.mobile);
                    $('.email').val(json.email);
                    var enroll = json.enroll == '0000-00-00 00:00:00' ? '' : json.enroll;
                    $('.enroll').val(enroll);
                    $('.university').val(json.university);
                    $('.college').val(json.college);
                }
            }
        }
    });

    $('.btn-save').on('click', function(){
        var params = {
            realname: $('.realname').val(),
            gender: $('input[name="gender"]:checked').val(),
            number: $('.number').val(),
            identity: $('.identity').val(),
            mobile: $('.mobile').val(),
            email: $('.email').val(),
            enroll: $('.enroll').val(),
            university: $('.university').val(),
            college: $('.college').val()
        };
        if (checkForm(params)) {
            $.ajax({
                type: 'post',
                url: '/v1/teacher/updateFile',
                headers: {
                    Authorization: authorization,
                    Accept: "application/json; charset=utf-8"
                },
                data: {params: params},
                success: function(e){
                    var data = JSON.parse(e);
                    if (data.status == 200) {
                        alert('个人资料更新成功!');
                        location.href = '/teacher/file.html';
                    }
                }
            });
        }
    });
});

function checkForm(params) {
    if (params.realname == '') {
        alert('请输入姓名');
        return false;
    }
    if (params.gender == '') {
        alert('请选择性别');
        return false;
    }
    if (params.number == '') {
        alert('请输入工号');
        return false;
    }
    if (params.identity == '') {
        alert('请输入身份证号');
        return false;
    }
    if (params.mobile == '') {
        alert('请输入手机号');
        return false;
    }
    if (params.email == '') {
        alert('请输入邮箱');
        return false;
    }
    if (params.enroll == '') {
        alert('请输入进校时间');
        return false;
    }
    if (params.university == '') {
        alert('请输入大学');
        return false;
    }
    return true;
}